
var checkList = document.getElementsByClassName('check-one check')
var checkAll = document.getElementsByClassName('check-all')[0]
//全选按钮
checkAll.onclick = function () {
    var state = checkAll.checked
    for (let i = 0; i < checkList.length; i++) {
        checkList[i].checked = state
    }
    getAllPrice()
}
//单选
for (let i = 0; i < checkList.length; i++) {
    checkList[i].onclick = function () {
        var flag = true
        for (let i = 0; i < checkList.length; i++) {
            if (!checkList[i].checked) {
                flag = false
                break
            }
        }
        checkAll.checked = flag
        getAllPrice()
    }
}

//加号绑定
var addList = document.getElementsByClassName('add')
for (let i = 0; i < addList.length; i++) {
    addList[i].onclick = function () {
        this.previousElementSibling.value++
        var price = this.parentElement.previousElementSibling.innerHTML * 1
        var getPrice = (price * this.previousElementSibling.value).toFixed(2)
        this.parentElement.nextElementSibling.innerHTML = getPrice
        this.previousElementSibling.previousElementSibling.innerHTML = '-'
        getAllPrice()
    }
}
//减号绑定
var reduceList = document.getElementsByClassName('reduce')
for (let i = 0; i < reduceList.length; i++) {
    reduceList[i].onclick = function () {
        if (this.nextElementSibling.value <= 1) { return }
        this.nextElementSibling.value--
        if (this.nextElementSibling.value <= 1) {
            this.innerHTML = ''
        }
        var price = this.parentElement.previousElementSibling.innerHTML * 1
        var getPrice = (price * this.nextElementSibling.value).toFixed(2)
        this.parentElement.nextElementSibling.innerHTML = getPrice
        getAllPrice()
    }
}
//删除绑定
var delList = document.getElementsByClassName('delete')
for (let i = 0; i < delList.length; i++) {
    delList[i].onclick = function () {
        this.parentElement.parentElement.remove()
        isCheck()
        getAllPrice()
    }
}
//删除选中
var deleteAll = document.getElementById('deleteAll')
deleteAll.onclick = function () {
    for (let i = 0; i < checkList.length; i++) {
        if (checkList[i].checked) {
            checkList[i].parentElement.parentElement.remove()
            i--
        }
    }
    isCheck()
    getAllPrice()

}
//做全选单选状态判定
function isCheck() {
    var flag = true
    for (let i = 0; i < checkList.length; i++) {
        if (!checkList[i].checked) {
            flag = false
            break
        }
        checkAll.checked = flag
    }
    if (checkList.length == 0) {
        checkAll.checked = false
    }
}
//结算
function getAllPrice() {
    var selectedTotal = document.getElementById('selectedTotal')
    var priceTotal = document.getElementById('priceTotal')
    var allNum = 0
    var allPrice = 0
    for (let i = 0; i < checkList.length; i++) {
        if (checkList[i].checked) {
            var num = checkList[i].parentElement.parentElement.getElementsByClassName('count-input')[0].value * 1
            var price = checkList[i].parentElement.parentElement.getElementsByClassName('subtotal')[0].innerHTML * 1
            allNum += num
            allPrice += price
        }
    }
    selectedTotal.innerHTML = allNum
    priceTotal.innerHTML = allPrice.toFixed(2)
}
